package cn.ubeta.b2c.utils;

import com.google.gson.internal.$Gson$Types;

import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public abstract class NetworkCallback<T> {

    final Type mType;

    public NetworkCallback() {
        mType = getSuperclassTypeParameter(getClass());
    }

    public Type getType() {
        return mType;
    }

    public abstract void onResponse(T result);
    public abstract void onFailure(IOException e);

    /**
     * 获取范型类型
     * @return
     */
    public static Type getSuperclassTypeParameter(Class<?> subclass) {
        Type superclass = subclass.getGenericSuperclass();
        if (superclass instanceof Class) {
            throw new RuntimeException("Missing type parameter");
        }
        ParameterizedType type = (ParameterizedType) superclass;
        return $Gson$Types.canonicalize(type.getActualTypeArguments()[0]);
    }

}
